<?php

declare (strict_types=1);
/**
 * 创建人 : Forpastime
 *
 * @link     http://hypercmf.com
 * @document https://hyperf.wiki
 * @contact  forpastime@qq.com
 * @git  https://gitee.com/hypercmf/hypercmf1.0.git
 */
namespace App\Model;

/**
 * @property int $id 
 * @property string $title 
 * @property string $description 
 * @property string $keywords 
 * @property string $thumbnail 
 * @property string $content 
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at 
 * @property string $status 
 */
class Article extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'article';

    public $timestamps = true;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    /**
     * 获取多条分页数据
     * @param $where
     * @param string $field
     * @param int $limit
     * @return mixed
     */
    public function getList($where,$field='*',$limit=10)
    {
        $res =  $this->where($where)->select($field)->paginate(intval($limit));
        if(!empty($res)){
            return for_paginator_data_formater($res);
        }else{
            return [];
        }
    }

    /**
     * 添加信息
     * @param $param
     */
    public function add($param)
    {
        $param['content'] = htmlspecialchars(htmlentities($param['content']));
        $param['description'] = htmlspecialchars(htmlentities($param['description']));
        $param['created_at'] = time();
        $result =  $this->insertGetId($param);
        if(false === $result){
            return false;
        }else{
            return $result;
        }
    }

    /**
     * 编辑信息
     * @param $param
     */
    public function edit($id,$param)
    {
        $result = $this->where('id', $id)->update($param);
        return $result;
    }

    /**
     * 根据id获取单条信息
     * @param $id
     */
    public function getOne($id)
    {
        $info = $this->where('id', $id)->first();
        if(empty($info)) return false;

        if(!empty($info)){
            return $info->toArray();
        }
    }

    /**
     * 删除
     * @param $id
     */
    public function del($id)
    {
        return $this->where('id', $id)->update(['status'=>0]);
    }
}